var reg = /^[0-9]*$/;

/*增加协议设计*/
function addProtocolDesign(designfuncs) {
    var username = "operator";
    var protocolName = window.sessionStorage.getItem("protocolName");
    var designName = $("#designName input[name='protocolName']").val();
    var info = $("#designInfo input[name='protocolInfo']").val();

    var auth = null;
    if (designfuncs.indexOf("auth") > -1) {
       auth={};
    }
    var encry = null;
    /********************************************************************数据加密***********************************************/
    if (designfuncs.indexOf("encry") > -1) {
        encry={};
    }

    var exception = null;
    /*********************************异常检测**********************************************/
    if (designfuncs.indexOf("exception") > -1) {
        exception={};
    }
    /***********************防病毒设计***********************************/
    var defend = null;
    if (designfuncs.indexOf("virus") > -1) {
        defend={};
    }

    var resend = null;
    /*********************************通信重传***************************************/
    if (designfuncs.indexOf("resend") > -1) {
        resend={};
    }

    var integrity = null;
    /****************************************完整性校验设计*******************************************/
    if (designfuncs.indexOf("integrity") > -1) {
        integrity={}
    }

    function stripscript(s)
    {
        var pattern = new RegExp("[`~!@#$^&*()=|{}':;',\\[\\].<>~！@#￥……&*（）——|{}【】‘；：”“'。，、？]");
        //格式 RegExp("[在中间定义特殊过滤字符]")
        var rs = "";
        for (var i = 0; i < s.length; i++) {
            rs = rs+s.substr(i, 1).replace(pattern, '');
        }
        return rs;
    }

    /*协议设计对象*/
    //用户名、协议名、安全协议名、备注、认证协商、加密协商与通信、异常检测、防病毒设计、重发设计、完整性设计、自定义设计

    var designObj = {};
    designObj.username = stripscript(config.username);
    designObj.protocolName = stripscript(protocolName);
    designObj.designName = stripscript(designName);
    designObj.other = stripscript(info);
    designObj.zTree={};

    var treeObj = $.fn.zTree.getZTreeObj("myDemo");
    if(treeObj.setting!="{}"&&treeObj.getNodes()!="{}"){
        designObj.zTree={
            setting:setting,
            nodes:treeObj.getNodes()
            //nodes:treeObj.transformTozTreeNodes(treeObj.getNodes())
            //nodes:treeObj.transformToArray(treeObj.getNodes())
        }
    }


    if (auth != null && auth != undefined ) {
        designObj.auth = auth;
    }else{
        designObj.auth = null;
    }

    if (encry != null && encry != undefined ) {
        designObj.encrypt = encry;
    }else {
        designObj.encrypt = null;
    }


    if (exception != null && exception != undefined ) {
        designObj.exception = exception;
    }else{
        designObj.exception = null;
    }

    if (defend != null && defend != undefined ) {
        designObj.virus = defend;
    }else{
        designObj.virus = null;
    }

    if (resend != null && resend != undefined ) {
        designObj.resend = resend;
    }else{
        designObj.resend = null;
    }

    if (integrity != null && integrity != undefined ) {
        designObj.integrity = integrity;
    }else{
        designObj.integrity = null;
    }

   /* if (defined != null && defined != undefined) {
        designObj.customField = defined;

    }*/

   console.log(designObj);



    $.ajax({
        url: HOST.concat("/protocolDesigns?token=") + config.token,
        type: "POST",
        dataType: "json",
        async: false,
        contentType: "application/json",
        data: JSON.stringify(designObj),
        success: function (res) {
            $("#saveField_save").attr("disabled",false);
            //console.log(res["data"]["id"]);
            checkLogin(res);
            alert("保存协议设计成功！");
            var address = "createConfiguration.html?id=" + res["data"]["id"];
            window.location.href = address;
        },
        error: function () {
            //只提示信息，不跳转
            alert("保存协议设计失败!");
        }

    });

}